import { isFunction } from  'lodash-es'
import type { AxiosResponse, InternalAxiosRequestConfig } from 'axios'
import type { HttpConfigType } from '@/service/http-type'
export function transInterceptors () {
  return {
    handleResponse: (opt: HttpConfigType, res: AxiosResponse) => {
      if(isFunction(opt?.dealResponse)) {
        return opt?.dealResponse(res)
      }else {
        if(opt.ReturnResData && (res?.config?.responseType === 'blob' || res?.config?.responseType === 'arraybuffer')) return res
        return res.data
      }
    },
    handleRequest: (opt: HttpConfigType, req: InternalAxiosRequestConfig) => {
      if(isFunction(opt?.dealRequest)) {
        return opt?.dealRequest(req)
      }else {
        return req
      }
    },
    handleResponseError(opt: HttpConfigType, error) {
      if(isFunction(opt?.dealResponseError)) {
        return opt?.dealResponseError(error)
      }else {
        return Promise.reject(error)
      }
    }
  }

}
